package com.gxa.modules.app.outpatient.selfpay.service.impl;


import com.gxa.common.utils.Result;
import com.gxa.common.utils.UUIDUtils;
import com.gxa.modules.app.outpatient.selfpay.dto.*;
import com.gxa.modules.app.outpatient.selfpay.mapper.SelfPayMapper;
import com.gxa.modules.app.outpatient.selfpay.service.SelfPayService;
import com.gxa.modules.model.entity.fee.Patient;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName PatientServiceImpl
 * @Description
 * @Author Mr.YUAN
 * @Date 2022/11/12 11:27
 */
@Service
public class SelfPayServiceImpl implements SelfPayService {

    @Autowired
    private SelfPayMapper selfPayMapper;

    @Override
    public List<SelfPaydto> queryById(Integer id) {
        List<SelfPaydto> selfPaydtos = selfPayMapper.queryById(id);
        return selfPaydtos;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Result addOutPayRecords(Integer id, Integer appointId, double orderMoney) {
//        String payPassword = selfPayMapper.getByUserId(userId);
        double balanceMoney = selfPayMapper.getMondyById(id);
        double lastMoney = balanceMoney - orderMoney;
        String orderId = "MZ" + UUIDUtils.createOnlyId();
        if (lastMoney < 0) {
            return new Result().error("缴费失败");
        }
        //先通过预约Id获取里面的信息
        BookRedisterDto bookRedisterDto = selfPayMapper.queryByRegisterId(appointId);
        //修改预约状态
        selfPayMapper.updateRegisterStatus(appointId);
        //修改就诊人余额（缴费）
        selfPayMapper.updateMoey(lastMoney,id);
        //存订单信息表
        OrderDetialAppdto orderDto = new OrderDetialAppdto();
        orderDto.setCardBalance(lastMoney)
                .setOrderId(orderId)
                .setOrderStatus(1)
                .setOrderTotal(bookRedisterDto.getOrderMoney())
                .setPayType(0);
        selfPayMapper.addOrderInfo(orderDto);
        //存就诊信息表
        OutPatientDateAppdto outDto = new OutPatientDateAppdto();
        outDto.setBusinessType(0)
                .setExpenseType(0)
                .setDeptName(bookRedisterDto.getDepartment())
                .setDoctorId(bookRedisterDto.getDoctorId())
                .setHospitalId(bookRedisterDto.getHospitalParkId());
        selfPayMapper.addConInfo(outDto);
        //存入门诊消费记录关系表信息
        OutPatientRecordsDto outRecords = new OutPatientRecordsDto();
        outRecords.setConsulationId(outDto.getId())
                .setOrderId(orderId)
                .setPatientId(id);
        selfPayMapper.addOutPatientRecord(outRecords);
        return new Result().ok(orderId);
    }

    @Override
    public Result queuryPayList(String orderId) {
        List<OrderPayListDto> orderPayListDtos = selfPayMapper.queryByPId(orderId);
        return new Result().ok(orderPayListDtos);
    }

    @Override
    public Result queuryPayLists(Integer id ,String startTime,String endTime) {
        List<OrderPayListDto> orderPayListDtos = selfPayMapper.queryByPatientId(id,startTime,endTime);
        return new Result().ok(orderPayListDtos);
    }
}